<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox, ElLoading } from 'element-plus'
import axios from '@/request/http'
import { useMainStore } from '@/store/main'
import yspglService from '@/api/yspgl'
import wjfwSerivce from '@/api/wjfw'
import detailItem from './components/detail-item.vue'

const store = useMainStore()

const emit = defineEmits(['ysplbChangeEvent'])

const file = ref()
// 入参

// 生命周期
onMounted(() => {
  //
})

const activeName = ref('first')

/*
 * 缩略图点击
 */
const fileClick = () => {
  //
}

const getWjxqxx = (data: any) => {
  if (Object.keys(data).length === 0) return (wjxqxxList.value = [])

  if (data.wjbh && data.wjbh.length !== 0) {
    wjxqxxList.value = [data]
    loadDetail(data)
  } else {
    wjxqxxList.value = []
  }

  // 缓存
  // cacheList.value = [cloneDeep(data)]
}

// 复选框绑定数据
const checkboxData = ref<any[]>([])
const wjxqxxList = ref<any>([])
/*
 * 全选
 */
const selectAll = () => {
  checkboxData.value = wjxqxxList.value
}

/*
 * 反选
 */
const selectInvert = () => {
  checkboxData.value = wjxqxxList.value.filter(
    (wj: any) => !checkboxData.value.find((el: any) => el.wjbh === wj.wjbh),
  )
}

/**
 * 上传文件
 */
const singleUploadFile = async (file: any) => {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('wjmc', '')
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  const res = await wjfwSerivce.upload(formData).catch((err) => {
    !err.isError && ElMessage({ type: 'warning', message: err.message })
    return {
      data: {},
    }
  })
  const resoult = await yspglService
    .uploadSxwj({
      file: res.wjbh,
      dabh: store.$state.nodeTreeData?.dabh,
      mlbh: store.$state.nodeTreeData?.bh,
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
      return {
        data: {},
      }
    })

  if (resoult.data === true) {
    ElMessage({ type: 'success', message: '上传文件成功' })
    loading.close()
    return resoult || ''
  }
}

/*
 * 上传文件（成功抛出刷新）
 */
const uploadClick = () => {
  file.value.multiple = false
  file.value.click()
}

// 上传文件（成功抛出刷新）
const uploadFile = async (e: any) => {
  const file = e.target.files[0]
  if (
    store.$state.nodeTreeData?.fbh === 'gd0020001' ||
    store.$state.nodeTreeData?.fbh === 'gd0020002'
  ) {
    if (
      !file.name.includes('.mp4') &&
      !file.name.includes('.avi') &&
      !file.name.includes('.wmv') &&
      !file.name.includes('.mpg') &&
      !file.name.includes('.mpeg') &&
      !file.name.includes('.aac') &&
      !file.name.includes('.ape')
    ) {
      ElMessage({ type: 'error', message: '只能上传音频或视频文件' })
      file.value.value = ''
      return
    }
  } else if (store.$state.nodeTreeData?.fbh === 'gd0020003') {
    if (
      !file.name.includes('.png') &&
      !file.name.includes('.jpg') &&
      !file.name.includes('.jpeg') &&
      !file.name.includes('.PNG') &&
      !file.name.includes('.JPG')
    ) {
      ElMessage({ type: 'error', message: '只能上传图片' })
      file.value.value = ''
      return
    }
  }
  const res = await singleUploadFile(file)
  emit('ysplbChangeEvent')
}

const checkChange = (data: any) => {
  checkboxData.value = data
}

/*
 * 刷新
 */
const refresh = () => {
  emit('ysplbChangeEvent')
}

/* 展示详情 */
const currentItem = ref<any>({})
const loadDetail = (item: any) => {
  currentItem.value = { ...item }
}

/* 关闭详情 */
const closeDetail = () => {
  currentItem.value = {}
}

/*
 * 删除（成功抛出刷新）
 */
const deleteWj = () => {
  ElMessageBox.confirm('是否删除选中的数据?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    const params = {
      dabh: checkboxData.value[0].dabh,
      mlbh: checkboxData.value[0].mlbh,
      wjbh: checkboxData.value[0].wjbh,
    }
    yspglService
      .deleteSxwj(params)
      .then((res) => {
        ElMessage({ type: 'success', message: '删除成功' })
        checkboxData.value = []
        emit('ysplbChangeEvent')
        wjxqxxList.value = []
        // // 操作成功后
        // cacheList.value.push(cloneDeep(wjxqxxList.value))
      })
      .catch((err) => {
        !err.isError && ElMessage({ type: 'warning', message: err.message })
      })
  })
}

//拖拽结束事件
const Onend = (val: any) => {
  const params = {
    ajbh: store.$state.nodeTreeData?.ajbh as string,
    mlbh: store.$state.nodeTreeData?.bh as string,
    wjsh: wjxqxxList.value.map((el: any, i: any) => {
      return {
        wjbh: el.wjbh,
        wjsxh: `${i + 1}`,
      }
    }),
  }
  // wsjzglService
  //   .updateWjsx(params)
  //   .then((res) => {
  //     // 操作成功后
  //     console.log(wjxqxxList.value.map((cur: any) => cur.wjbh))
  //     cacheList.value.push(cloneDeep(wjxqxxList.value))
  //   })
  //   .catch((err) => {
  //     !err.isError && ElMessage({ type: 'warning', message: err.message })
  //   })
}

const checked = ref(true)

/*
 * 上一个
 */
// const lastFile = () => {
// }

/*
 * 下一个
 */
// const nextFile = () => {
// }

defineExpose({ getWjxqxx, closeDetail })
</script>
<template>
  <div class="container">
    <el-tabs v-model="activeName" class="demo-tabs" type="card">
      <el-tab-pane label="音像" name="first">
        <div class="content">
          <!-- 展示详情 -->
          <detail-item
            :current-item="currentItem"
            class="detail-wrapper"
            @close="closeDetail"
            v-if="currentItem.wjbh"
          ></detail-item>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  .content {
    width: 100%;
    height: 100%;
    padding: 5px;
    display: flex;
    flex-direction: column;
    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #dae4f1;
      padding-bottom: 5px;
      margin-bottom: 5px;
      .topLeft {
        display: flex;

        img {
          padding-right: 5px;
        }
        p {
          font-weight: 600;
        }
      }
    }
    .buttomGroup {
      width: 100%;
      display: flex;
      justify-content: space-between;
      margin-bottom: 5px;
      .rowbutton {
        display: flex;
      }
    }

    .center {
      padding: 15px;
      background-color: #f7f8f9;
      // flex: 1;
      // flex-shrink: 0;
      height: 650px;
      overflow: auto;
      display: flex;
      flex-direction: column;
      .headerButtomGroup {
        .header-text {
          color: #4a76ab;
        }
        .header-text + .header-text {
          margin-left: 10px;
        }
        .header-avtive {
          color: #fff;
          background-color: #3c89d0;
        }
      }

      .writingsBox {
        overflow-y: auto;
        max-height: 100%;
        display: flex;
        flex-wrap: wrap;
        margin-top: 10px;
        .writingsItem {
          width: 212px;
          padding: 0 16px 10px 0;
          display: flex;
          flex-direction: column;

          .item-content {
            display: flex;
            flex-direction: column;
            flex: 1;
            flex-shrink: 0;
            background-color: #fff;
            box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
            cursor: pointer;
            .content-header {
              display: flex;
              justify-content: space-between;
              align-items: center;
              padding: 0 10px;
              img {
                width: 15px;
                height: 15px;
              }
            }

            .content-img {
              margin: 0 10px 10px 10px;
              flex: 1;
              flex-shrink: 0;
              background-color: #b3c6d1;
              display: flex;
              justify-content: center;
              align-items: center;

              // & > img {
              //   width: 100%;
              //   height: 194px;
              // }
            }

            .photo-imgs {
              margin: 0 10px 10px 10px;
              flex: 1;
              flex-shrink: 0;
              img {
                width: 100%;
                height: 100%;
              }
            }

            .content-footer {
              width: 100%;
              text-align: center;
              padding: 5px 0;
              background-color: #f7c863;
            }
          }

          .pageNumber {
            height: 26px;
            line-height: 26px;
            text-align: center;
          }
        }
      }
    }

    .bottomButton {
      margin-top: 10px;
    }
  }
}

.export-excel {
  li {
    text-align: center;
    height: 30px;
    line-height: 30px;
    cursor: pointer;
    &:hover {
      background-color: #ecf5ff;
      color: #266df5;
    }
  }
}
.detail-wrapper {
  width: 100%;
  height: 100%;
  background-color: #fff;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
}
</style>
